﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Liya.Crm.IService;
using System.Web.Security;
using Liya.Crm.Utility;
using Liya.Crm.Web.Models;
using Liya.Crm.Data.Models;

namespace Liya.Crm.Web.Controllers
{
    public class HomeController : BaseController
    {
        protected IFunction _functionService;

        public HomeController(IFunction functionService)
        {
            _functionService = functionService;
        }

        [Authorize]
        public ActionResult Index()
        {
            //获取用户权限
            if (User.Identity.IsAuthenticated)
            {
                UserData userData = LoginUser;
                IEnumerable<Sys_Function> functions = _functionService.GetFunctionsByRoles(userData.Roles);

                //菜单Data
                //获取一级菜单
                IEnumerable<Sys_Function> first = functions.Where(p => p.FParentId == 0);
                ViewData["FirstMenu"] = first;
                Dictionary<int, IEnumerable<Sys_Function>> second = new Dictionary<int, IEnumerable<Sys_Function>>();
                foreach (Sys_Function item in first)
                {
                    second.Add(item.FId,
                                    functions.Where(p=>p.FParentId==item.FId));
                }
                ViewData["SecondMenu"] = second;
            }
            return View();
        }

        public ActionResult Desktop()
        {
            ViewData["LoginUser"] = LoginUser.UserName;
            return View();
        }
    }
}
